﻿let Area
let parentCategoryID
let currentPage
let StoreCategorysID
let RegionID

$(function () {
    Area = getArea()
    parentCategoryID = getParentCategoryID()
    currentPage = getCurrentPage()
    StoreCategorysID = getStoreCategorysID()
    RegionID = getRegionID()
    addEventListener()
})

async function amountChange(a) {
    cityID = 0
    currentPage = 1
    Area = a
    await dataLoad()
    addEventListener()
}

async function nextpage() {
    console.log("加一页")
    currentPage += 1
    await dataLoad()
}

async function beforepage() {
    console.log("减一页")
    currentPage -= 1
    await dataLoad()
}
async function categoryChange(id) {
    RegionID = 0
    currentPage = 1
    StoreCategorysID = id
    // 添加新的div元素  
    //var newDiv = document.createElement("div");
    //newDiv.innerHTML = "新添加的分类信息: " + id; // 你可以根据需要修改这里的内容  
    //newDiv.className = "newSearchTag"; // 为新div添加一个类名以便样式化

    //// 将新div添加到页面上  
    //var new= document.getElementsByClassName("newTagContainer");
    //new.appendChild(newDiv);

    //// 如果需要，显示这个新容器  
    //document.getElementById("newTagContainer").style.display = "block"; 
    await dataLoad()
}

function updateStoreItems() {
    var storeItems = document.querySelectorAll('#area');
    console.log(storeItems);
    storeItems.forEach(function (item) {
        var area1 = item.getAttribute('data-area');
        console.log(area1);
        var displayText = updateAreaDisplay(area1); // 确保这个函数已定义  
        console.log(displayText);
        item.querySelector('span').textContent = displayText;
        console.log(item.querySelector('span').textContent+'1');
    });
} 

document.addEventListener('DOMContentLoaded', function () {
    var storeItems = document.querySelectorAll('#area');
    console.log(storeItems)
    storeItems.forEach(function (item) {
        var area1 = item.getAttribute('data-area');
        var displayText = updateAreaDisplay(area1);
        item.querySelector('span').textContent = displayText;
    });
});

function updateAreaDisplay(area1) {
    if (area1 === '1') {
        return "20平米以下";
    } else if (area1 === '2') {
        return "20-50平米";
    }
    else if (area1 === '3') {
        return "50-100平米";
    }
    else if (area1 === '4') {
        return "100-200平米";
    }
    else if (area1 === '5') {
        return "200-500平米";
    }
    else if (area1 === '6') {
        return "500平米以上";
    }
    return "未知面积"; // 或者你可以根据需要返回其他值  
}

async function dataLoad() {
    let url = `page=${currentPage}&StoreCategorysID=${StoreCategorysID}&Area=${Area}&parentCategoryID=${parentCategoryID}&RegionID=${RegionID}`;
    let obj = {
        page: currentPage,
        StoreCategorysID: StoreCategorysID,
        Area: Area,
        parentCategoryID: parentCategoryID,
        RegionID: RegionID
    }
    let jsonStr = JSON.stringify(obj)
    jsonStr = btoa(jsonStr)//base64编码
    jsonStr = encodeURIComponent(jsonStr)//URL编码
    await $.ajax({
        url: '/Search/Index/' + jsonStr,
        success: async function (e) {
            var tp = $(".serch-content");
            var np = $(e);
            tp.children().remove();
            np.find(".serch-content").children().appendTo($(".serch-content"));
        }
    })
    updateStoreItems()
}

async function cityChange(a) {
    currentPage = 1
    RegionID = a
    let url = `page=${currentPage}&StoreCategorysID=${StoreCategorysID}&Area=${Area}&parentCategoryID=${parentCategoryID}&RegionID=${RegionID}`;
    let obj = {
        page: currentPage,
        StoreCategorysID: StoreCategorysID,
        Area: Area,
        parentCategoryID: parentCategoryID,
        RegionID: RegionID
    }
    let jsonStr = JSON.stringify(obj)
    jsonStr = btoa(jsonStr)//base64编码
    jsonStr = encodeURIComponent(jsonStr)//URL编码
    await $.ajax({
        url: '/Search/Index/' + jsonStr,
        success: async function (e) {
            ``
            var tp = $(".transferContext");
            var np = $(e);
            tp.children().remove();
            np.find(".transferContext").children().appendTo($(".transferContext"));
        }
    })
    $("#citybox-div a>span").removeClass("selected")
    $("#span-city-" + RegionID).addClass("selected")
}

function getArea()
{
    let v = $("#initArea").val()
    if (v == null) { v = 0; }
    return v;
}

function getParentCategoryID()
{
    let v = $("#parentCategoryID").val()
    if (v == null) { v = 0; }
    return v;
}

function getCurrentPage()
{
    let v = $("#currentpage").val()
    if (v == null) { v = 0; }
    return v;
}

function getStoreCategorysID()
{
    let v = $("#initCategoryID").val()
    if (v == null) { v = 0; }
    return v;
}

function getRegionID()
{
    let v = $("#initCityID").val()
    if (v == null) { v = 0; }
    return v;
}